<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta content="width=device-width, initial-scale=1.0" name="viewport">
    <title>自动回复关键字</title>
    <link href="../../resource/css/bootstrap.min.css?v=3.3.5" rel="stylesheet"
          th:href="@{/resource/css/bootstrap.min.css?v=3.3.5}"/>
    <link href="../../resource/css/font-awesome.min.css?v=4.4.0" rel="stylesheet"
          th:href="@{/resource/css/font-awesome.min.css?v=4.4.0}"/>
    <!-- Morris -->
    <link href="../../resource/css/animate.min.css" rel="stylesheet" th:href="@{/resource/css/animate.min.css}"/>

    <link href="../../resource/hotui/css/style.min-1.0.8.css" th:href="@{/resource/hotui/css/style.min-1.0.8.css}"
          rel="stylesheet">
    <link href="../../resource/hotui/css/hotui.style.override.css"
          rel="stylesheet" th:href="@{/resource/hotui/css/hotui.style.override.css}"/>
    <link href="../../resource/css/style-diy.css?v=4.0.0" rel="stylesheet"
          th:href="@{/resource/css/style-diy.css?v=4.0.0}"/>
    <link href="../../resource/hotui/js/plugins/datetimepick/css/daterangepicker.css"
          rel="stylesheet" th:href="@{/resource/hotui/js/plugins/datetimepick/css/daterangepicker.css}"/>


    <style>
        /*.daterangepicker {*/
        /*    z-index: 999999999 !important;*/
        /*}*/
        .form-group-box {
            display: inline-block;
            width: 200px;
        }
    </style>
</head>
<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <!--search-->
    <form class="search-panel" id="searchForm" method="get" role="form" th:object="${condition}">
        <div class="row">
            <div class="col-lg-12">
                <div class="ibox">
                    <div class="ibox-content">
                        <div class="form-inline">
                            <input name="pageIndex" th:value="${pageIndex}" type="hidden"/>
                            <div class="form-inline">
                                <div class="form-group m-r-sm">
                                    <label class=" control-label" for="keyword">关键字：</label>
                                    <input class="form-control input-sm mb10" id="keyword" name="keyword"
                                           placeholder="关键字"
                                           style="width: 156px;" th:value="*{keyword}" type="text"/>
                                </div>
                                <div class="form-group m-r-sm">
                                    <button class="btn btn-primary btn-sm m-b-none searchBtn"
                                            onclick="listHandler.search(1)"
                                            type="button">筛选
                                    </button>
                                    <button class="btn btn-primary btn-sm m-b-none" onclick="listHandler.searchAll()"
                                            type="button">显示全部
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
    <!--search-->


    <!--list-->
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins m-b-none">
                <div class="tabs-container">
                    <div class="tab-content">
                        <div class="tab-pane active">
                            <div class="panel-body table-responsive">
                                <div class="form-inline" style="padding-bottom: 15px;text-align: right;">
                                    <button type="button" onclick="listHandler.createModalShow()"
                                            class="btn btn-primary btn-sm m-b-none">添加关键字
                                    </button>
                                </div>
                                <table class="table table-bordered table-hover table-center">
                                    <thead>
                                    <tr>
                                        <th>序号</th>
                                        <th>关键字</th>
                                        <th>回复类型</th>
                                        <th>回复内容</th>
                                        <th>创建时间</th>
                                        <th width="150">操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <tr th:each="item,stat:${list}">
                                        <td th:text="${stat.count}"></td>
                                        <td th:text="${item.keyword}"></td>
                                        <td>
                                            <span th:if="${item.msgType.code==0}">文本</span>
                                            <span th:if="${item.msgType.code==1}">图片</span>
                                        </td>
                                        <td>
                                            <th:block th:if="${item.msgType.code==0}">
                                                <span th:text="${item.content}"></span>
                                            </th:block>
                                            <th:block th:if="${item.msgType.code==1}">
                                                <img th:src="${item.url}" style="width: 100px;height: 50px">
                                            </th:block>
                                        </td>
                                        <td th:text="${#temporals.format(item.createTime,'yyyy-MM-dd HH:mm:ss')}"></td>
                                        <td style="text-align: center">
                                            <button class="btn btn-default btn-xs m-b-none"
                                                    th:onclick="listHandler.delete([[${item.id}]])"
                                                    type="button">删除
                                            </button>
                                        </td>
                                    </tr>
                                    </tbody>
                                </table>
                                <p style="float: left; margin: 20px 0;">
                                    共<span th:text="${totalRecords}"></span>条记录，当前第<span th:text="${pageIndex}"></span>/<span
                                        th:text="${totalPages==0?1:totalPages}"></span>，每页20条记录
                                </p>
                                <ul class="pagination pagination-split" style="float: right;">
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<input class="file" type="file" id="uploadfileVideo" name="img"
       style="display: none;"
       accept="image/*" view-element=".videoPreview"
       val-element="#msgUrl"/>
<script type="text/html" id="create_video_template">
    <div class="form-horizontal" style="width: 600px;">
        <div class="form-group">
            <label class="col-sm-2 control-label">关键字:</label>
            <div class="col-sm-10 form-group-box">
                <input id="msgKeyword" class="form-control" placeholder="回复关键字">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">回复类型:</label>
            <div class="col-sm-10 form-group-box">
                <select id="msgTye" class="form-control">
                    <option value="0">文本</option>
                    <option value="1">图片</option>
                </select>
            </div>
        </div>
        <div class="form-group" id="content-box">
            <label class="col-sm-2 control-label">回复内容:</label>
            <div class="col-sm-10 form-group-box" style="width: 400px;">
                <textarea placeholder="自动回复内容" name="msgContent" id="msgContent"
                          class="form-control"
                          rows="5" style="height: 56px;"></textarea>
            </div>
        </div>
        <div class="form-group" style="display: none" id="upload-box">
            <label class="col-sm-2 control-label" style="padding-top:70px;">回复图片:</label>
            <div class="col-sm-10 form-group-box">
                <input type="hidden" name="msgUrl" id="msgUrl"/>
                <a title="选择图片">
                    <img class="videoPreview" src="../../resource/img/bg.png"
                         onerror="this.src='/resource/img/bg.png'"
                         style="margin-top: 10px; width:150px;height:150px;"
                    />
                </a>
            </div>
        </div>
    </div>
</script>

<!--基础框架js-->
<script src="../../resource/js/jquery.min2.2.4.js?v=2.1.4" th:src="@{/resource/js/jquery.min2.2.4.js?v=2.1.4}"></script>
<script src="../../resource/js/bootstrap.min.js?v=3.3.5" th:src="@{/resource/js/bootstrap.min.js?v=3.3.5}"></script>
<script src="../../resource/hotui/js/hot/bootstrap.hot.extra-utils.js"
        th:src="@{/resource/hotui/js/hot/bootstrap.hot.extra-utils.js?v=1.0.0}"></script>
<script src="../../resource/hotui/js/hot/bootstrap.hot.extra-init.js"
        th:src="@{/resource/hotui/js/hot/bootstrap.hot.extra-init.js}"
></script>
<script src="../../resource/lib/moment.min.js" th:src="@{/resource/lib/moment.min.js}"></script>
<script src="../../resource/hotui/js/plugins/datetimepick/js/daterangepicker.js"
        th:src="@{/resource/hotui/js/plugins/datetimepick/js/daterangepicker.js}"></script>

<script src="../../resource/hotui/js/plugins/ajaxfileupload.js"
        th:src="@{/resource/hotui/js/plugins/ajaxfileupload.js}"></script>


<script th:inline="javascript" type="text/javascript">
    const pageIndex = /*[[${pageIndex}]]*/  1;
    const totalPages = /*[[${totalPages}]]*/  1;
    const pageUri = /*[[@{/wx/keyword/list}]]*/  "";
    const apiUri = /*[[@{/wx/keyword/list}]]*/  "";
    const uploadUri = /*[[@{/resource/upload}]]*/  "";
</script>
<script>
    let createModal = $("#create_video_template").modal("添加自动回复关键字", function () {
        listHandler.create();
    });
    $(function () {
        //初始化分页
        var pageinate = new hot.paging(".pagination", pageIndex, totalPages, 7);

        pageinate.init(function (p) {
            listHandler.search(p);
        });

        $("#uploadfileVideo").change(function () {
            let $view = $(this).attr('view-element');
            let $val = $(this).attr('val-element');
            hot.loading.show();
            hot.fileUpload(uploadUri, $(this).attr('id'), null, function (apiResult) {
                hot.loading.close();
                if (apiResult.resultCode === 2000) {
                    $($view).attr("src", apiResult.data.previewUrl);
                    $($val).val(apiResult.data.previewUrl);
                }
            });
        });


    });
    let listHandler = {
        search: function (pageIndex) {
            $("input[name=pageIndex]").val(pageIndex);
            $("#searchForm").submit();
        },
        searchAll: function () {
            window.location.href = pageUri;
        },
        createModalShow: function () {
            let options = {
                btn: ["保存关键字", "取消"]
            };
            createModal.show(function () {
                //上传
                $('.videoPreview').click(function () {
                    $("#uploadfileVideo").click();
                });

                $("#msgTye").change(function () {
                    if (parseInt(this.value) === 0) {
                        $("#upload-box").hide();
                        $("#content-box").show();

                    } else {
                        $("#upload-box").show();
                        $("#content-box").hide();
                    }
                });
            }, options);
        },
        create: function () {
            let keyword = $("#msgKeyword").val();
            let content = $("#msgContent").val();
            let type = $("#msgTye").val();
            let url = $("#msgUrl").val();
            if (typeof keyword === "undefined" || keyword === "") {
                hot.tip.error("请输入回复关键字");
                return;
            }
            if (parseInt(type) === 0) {
                if (typeof content === "undefined" || content === "") {
                    hot.tip.error("请输入自动回复内容");
                    return;
                }
            } else {
                if (typeof url === "undefined" || url === "") {
                    hot.tip.error("请上传自动回复的图片");
                    return;
                }
            }

            let requestData = {
                keyword, content, type, url
            };

            hot.ajax(apiUri + "/save", requestData, function (apiResult) {
                if (apiResult.resultCode === 2000) {
                    hot.tip.success("保存成功", function () {
                        window.location.reload();
                    }, 200)
                } else {
                    hot.tip.error("保存失败--" + apiResult.resultMsg);
                }
            }, function () {
            }, "post", 100);
        },
        delete(id) {
            hot.confirm("是否删除当前关键字消息？", function () {
                hot.ajax(apiUri + "/delete", {id: id}, function (apiResult) {
                    if (apiResult.resultCode === 2000) {
                        hot.tip.success("删除成功", function () {
                            window.location.reload();
                        }, 200);
                    } else {
                        hot.tip.error("删除失败");
                    }
                }, function () {
                }, "get", 100);
            });
        }
    }
</script>
</body>
</html>